﻿using BYSJ.BLL;
using BYSJ.IBLL;
using BYSJ.Model;
using BYSJ.Web.Commmend;
using BYSJ.Web.Filters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BYSJ.BLL;

namespace BYSJ.Web.Controllers
{
    public class FileInputController : Controller
    {
        IFileMangSetService fileServer = null;
        IUserSetService user = null;
        public FileInputController()
        {
            fileServer = new FileMangSetService();
            user = new UserSetService();
        }
        //
        // GET: /FileInput/ 学生共享资源
        [AdminFilter]
        public string Index()
        {
            var biaoshi = true;
            for (int i = 0; i < Request.Files.Count; i++)
			{
                var fileobj = Request.Files[i];
                FileMangSet file = new FileMangSet();
                file.FilecreatTime = DateTime.Now;
                file.FileId =Convert.ToInt32(Request.Cookies["cookiename"].Value);
                file.FileIsdel = false;
                file.Fileleibie = 5;
                file.Fileleibiename = "共享资源";
                file.FileName = fileobj.FileName;
                file.FileUpdatetime = DateTime.Now;
                file.FileUrl = "http://cnkifile.b0.upaiyun.com/bysjdefort/shaerfile/" + fileobj.FileName;
                fileServer.Add(file);
                biaoshi=writeFileyun(fileobj.FileName,fileobj.InputStream);
                if (biaoshi == false)
                {
                    break;
                }
			}
            if(biaoshi==true)
            {
                return "<script>alert('上传成功');self.location=document.referrer;</script>"; 
            }

            else
            {
                return "<script>alert('上传失败！');self.location=document.referrer;</script>"; 
            }        
        }
        public static bool writeFileyun(string name,Stream filestream)
        {
            UpYun upyun = new UpYun("cnkifile", "senhong", "sen123456");
            BinaryReader r = new BinaryReader(filestream);
            byte[] filebute = r.ReadBytes((int)filestream.Length);
            return upyun.writeFile("/bysjdefort/shaerfile/" + name, filebute, true);
        }

    }
}
